<?php

namespace services\baibai;

use Yii;
use common\models\base\BbBodyParts;
use common\models\patient\MonthRevisitingRecord;
use common\models\patient\RevisitingRecord;
use common\models\clinic\Clinic;
use common\models\base\PatientDiseases;
use common\models\clinic\Project;
use common\models\clinic\TreatmentRoom;
use common\models\member\Attr;
use common\models\member\Member;
use common\models\patient\ConsumptionDetail;
use common\models\clinic\UserTreatmentDetail;
use common\models\clinic\UserSurplus;
use common\models\clinic\UserTreatment;
use common\models\patient\ClinicRecord;
use common\models\patient\Consumption;
use common\models\patient\MedicalRecords;
use common\models\patient\User;
use common\models\baibai\Model;
use common\components\Service;
use yii\db\Expression;


/**
 * 白白管家数据迁移
 *
 * Class DataMigrateService
 * @package services\baibai
 */
class DataMigrateService extends Service
{
    public static $projectId = 13;

    /**
     * 全部数据
     */
    public static function allData()
    {
        $lastPatient = User::find()
            ->select('id')
            ->orderBy('id desc')
            ->limit(1)
            ->asArray()
            ->one();
        $lastPatientId = 0;
        if ($lastPatient && $lastPatient['id']) {
            $lastPatientId = $lastPatient['id'];
        }
        $transaction = Yii::$app->db->beginTransaction();
        try {
            // 部位同步
            self::createBbBodyParts();

            // 患者治疗
            self::treat();

            // 患者治疗项目
            self::userSurplus();

            // 患者消费记录表
            self::patientConsumption();

            // 患者就诊记录表(患者到院)
            self::clinicRecord($lastPatientId);

            // 患者病历数据
            self::case($lastPatientId);

            // 同步患者数据
            self::patient($lastPatientId);

            // 患者消费、来院记录表
            self::patientRevisitingRecordBill();
            // 治疗、来院记录表
            self::patientRevisitingRecordTreat();
            // 更新是否统计
            self::updateIsSum();

            $transaction->commit();
        } catch (\Exception $exception) {
            $transaction->rollBack();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * 同步患者数据
     * @Interface patient
     */
    public static function patient($lastPatientId)
    {
        Model::setTableName('bm_patient');
        $baiPatient = Model::find()->where(['>', 'id', 0])->asArray()->all();

        $addList = [];
        foreach ($baiPatient as $val) {

            // 患者ID 为3,4,5 和 8,9 的诊疗号相同，在白友系统里诊疗号唯一，所以得修改
            if (in_array($val['id'], [3,4,5,8,9])) {
                $val['treat_no'] = self::havePatient($val['id']);
            }

            $patientUserIs = User::findOne(['patient_card_id' => $val['treat_no']]);
            if (!$patientUserIs) {
                $add['id'] = $lastPatientId + $val['id'];
                $add['project_id'] = self::$projectId;
                $add['patient_card_id'] = $val['treat_no'];
                $add['name'] = $val['real_name'];
                $add['birthday'] = $val['birth_date'];
                $add['age'] = self::getAgeByBirthDate($val['birth_date']);
                $add['wx_name'] = ''; //
                $add['nickname'] = ''; //
                $add['source_channel_id'] = 0;
                $add['gender'] = $val['gender'];
                $add['mobile'] = $val['phone'];
                $add['city_id'] = $val['city_id'];
                $add['city_name'] = $val['city_name'];
                $add['address'] = $val['address'];
                $add['patient_diseases_id'] = self::getPatientDisease($val['id']);
                $add['patient_body_parts_name'] = self::getPatientBodyParts($val['id']);
                $add['patient_tags_name'] = '';
                $add['belong_assistant_id'] = 0;
                $add['belong_member_id'] = 0;
                $add['belong_member_name'] = '';
                $add['effect_type_id'] = 0;
                $add['risk_type_id'] = 0;
                $add['clinic_assess_id'] = 0;
                $add['outpatient_type_id'] = 1055; // 患者就诊状态最初为初诊
                $add['last_clinic_time'] = self::getPatientLastClinicTime($val['id']);
                $add['clinic_id'] = self::getPatientClinicOrReg($val['id'], 1);
                $add['profession'] = '';
                $add['reg_time'] = self::getPatientClinicOrReg($val['id'], 2);
                $add['last_revisiting_time'] = $val['recept_time'];
                $add['kf_next_callback_time'] = 0;
                $add['kf_last_callback_time'] = 0;
                $add['kf_last_callback_user'] = '';
                $add['kf_callback_count'] = 0;
                $add['yz_last_callback_time'] = 0;
                $add['yz_next_callback_time'] = 0;
                $add['yz_last_callback_user'] = '';
                $add['yz_callback_count'] = 0;
                $add['next_revisiting_time'] = 0;
                $add['last_callback_time'] = 0;
                $add['next_callback_time'] = 0;
                $add['callback_count'] = 0;
                $add['is_anti_relapse'] = 0;
                $add['missed_days'] = (time() - $val['recept_time']) / 86400;
                $add['revisiting_count'] = self::patientRevisitingCount($val['id']);
                $add['remark'] = '';
                $add['status'] = 1;
                $add['modify_time'] = 0;
                $add['created_at'] = time();
                $add['updated_at'] = time();
                $addList[] = $add;
            }
        }

        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(User::tableName(),
                    [
                        'id', 'project_id', 'patient_card_id', 'name', 'birthday', 'age', 'wx_name',
                        'nickname', 'source_channel_id', 'gender', 'mobile', 'city_id', 'city_name',
                        'address', 'patient_diseases_id', 'patient_body_parts_name',
                        'patient_tags_name', 'belong_assistant_id', 'belong_member_id',
                        'belong_member_name', 'effect_type_id', 'risk_type_id', 'clinic_assess_id', 'outpatient_type_id',
                        'last_clinic_time', 'clinic_id', 'profession', 'reg_time', 'last_revisiting_time',
                        'kf_next_callback_time', 'kf_last_callback_time', 'kf_last_callback_user', 'kf_callback_count', 'yz_last_callback_time',
                        'yz_next_callback_time', 'yz_last_callback_user', 'yz_callback_count', 'next_revisiting_time', 'last_callback_time',
                        'next_callback_time', 'callback_count', 'is_anti_relapse', 'missed_days', 'revisiting_count',
                        'remark', 'status', 'modify_time', 'created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }
        return true;
    }

    /**
     * 根据出生日期获取年龄
     * @param $birthDate string 出生日期【格式：2019-01-01】
     * @return float|int
     */
    public static function getAgeByBirthDate($birthDate)
    {
        if (!$birthDate) {
            return 0;
        }

        list($year, $month, $day) = explode('-', $birthDate);
        list($nowYear, $nowMonth, $nowDay) = explode('-', date('Y-m-d'));

        $diffYear = $nowYear - $year;
        $diffMonth = $nowMonth - $month;
        $diffDay = $nowDay - $day;
        // 月份不足or天份不足
        if ($diffMonth < 0 || ($diffMonth >= 0 && $diffDay < 0)) {
            $diffYear--;
        }
        return $diffYear >= 0 ? $diffYear : 0;
    }

    /**
     * 获取患者病种
     *
     * @Interface getPatientDisease
     */
    public static function getPatientDisease($patientId)
    {
        Model::setTableName('bm_case_record');
        $patientDisease = Model::find()
            ->select('patient_id, disease_id')
            ->where(['patient_id' => $patientId])
            ->orderBy('id desc')
            ->limit(1)
            ->asArray()
            ->one();
        if ($patientDisease) {
            return $patientDisease['disease_id'];
        }
        return 0;
    }

    /**
     * 获取患者治疗部位
     *
     * @Interface getPatientBodyParts
     * @param $patientId
     * @return mixed|string
     */
    public static function getPatientBodyParts($patientId)
    {
        Model::setTableName('bm_treat_report');
        $patientParts = Model::find()
            ->select('patient_id, part')
            ->where(['patient_id' => $patientId])
            ->orderBy('id desc')
            ->limit(1)
            ->asArray()
            ->one();
        if ($patientParts) {
            return $patientParts['part'];
        }
        return '';
    }

    /**
     * 最后一次治疗信息
     *
     * @Interface getPatientLastClinicTime
     * @param $patientId
     * @return int|mixed
     */
    public static function getPatientLastClinicTime($patientId)
    {
        Model::setTableName('bm_treat');
        $patientClinicTime = Model::find()
            ->select('patient_id, finished_at')
            ->where(['patient_id' => $patientId])
            ->andWhere(['>', 'finished_at', 0])
            ->orderBy('id desc')
            ->limit(1)
            ->asArray()
            ->one();
        if ($patientClinicTime) {
            return $patientClinicTime['finished_at'];
        }
        return 0;
    }

    /**
     * 患者诊室(预约表最后一条诊室) / 患者第一次到诊(预约表第一条创建时间) 状态在1,3中
     *
     * @Interface getPatientClinicOrReg
     * @param $patientId
     * @return int|mixed
     */
    public static function getPatientClinicOrReg($patientId, $order)
    {
        if ($order == 1) {
            $orserIs = 'id desc';
        } else {
            $orserIs = 'id asc';
        }
        Model::setTableName('bm_patient_appoint_info');
        $patientClinicOrReg = Model::find()
            ->select('patient_id, clinic_id, clinic_name, created_at')
            ->where(['patient_id' => $patientId, 'appoint_status' => [1, 3]])
            ->orderBy($orserIs)
            ->limit(1)
            ->asArray()
            ->one();
        if ($patientClinicOrReg) {
            if ($order == 1) {
                $clinicIs = Clinic::findOne([
                    'name' => $patientClinicOrReg['clinic_name'],
                    'project_id' => 13,
                ]);
                // 患者诊室(预约表最后一条诊室)，诊室名称和白白管家一致时，返回白友诊室ID
                if ($clinicIs) {
                    return $clinicIs->id;
                }
                return 0;
            } else {
                return $patientClinicOrReg['created_at'];
            }
        }
        return 0;
    }

    /**
     * 患者来院次数
     *
     * @Interface patientRevisitingCount
     * @param $patientId
     * @return int|string
     */
    public static function patientRevisitingCount($patientId)
    {
        Model::setTableName('bm_patient_appoint_info');
        return Model::find()
            ->select('id')
            ->where(['patient_id' => $patientId, 'appoint_status' => [1, 3]])
            ->count();
    }

    //=====================================================================================

    /**
     * 患者部位关联
     *
     * @Interface bodyParts
     * @param $patientId
     * @return array|void|\yii\db\ActiveRecord[]
     */
    public static function bodyParts($patientId)
    {
        Model::setTableName('bm_patient');
        $baibaiPatient = Model::findOne($patientId);

        // 白友的患者ID
        $baiyouPatient = User::findOne(['patient_card_id' => $baibaiPatient->treat_no]);
        if (!$baiyouPatient) {
            return;
        }

        Model::setTableName('bm_patient_attach');
        $list = Model::find()
            ->select('id')
            ->where(['patient_id' => $patientId])
            ->asArray()
            ->all();
        $attachIds = array_column($list, 'id');

        Model::setTableName('bm_patient_attach_info');
        $bodyLarge = Model::find()
            ->select('body_large_id')
            ->where(['patient_attach_id' => $attachIds])
            ->asArray()
            ->all();
        $bodyLargeIds = array_unique(array_column($bodyLarge, 'body_large_id'));

        foreach ($bodyLargeIds as $val) {
            $add['patient_user_id'] = $baiyouPatient->id;
            
            $add['body_parts_id'] = $baiyouPatient->id;
        }
        return $list;
    }


    //===============================================================================================

    /**
     * 患者病历数据
     */
    public static function case($lastPatientId)
    {
        Model::setTableName('bm_case_record');
        $baibaiCase = Model::find()
            ->select('patient_id, id')
            ->groupBy('patient_id')
            ->asArray()
            ->all();

        $addList = [];
        foreach ($baibaiCase as $val) {
            Model::setTableName('bm_case_record');
            $caseIs = Model::find()
                ->where(['patient_id' => $val['patient_id']])
                ->orderBy('id desc')
                ->limit(1)
                ->asArray()
                ->one();
            $add['project_id'] = self::$projectId;
            $add['patient_user_id'] = $lastPatientId + $val['patient_id'];
            $add['sick_time'] = $caseIs['ill_time'];
            $add['main_desc'] = $caseIs['main_complain'];
            $add['now_medical_history'] = $caseIs['present_history'];
            $add['allergy_history'] = $caseIs['allergy_history'];
            $add['jw_history'] = $caseIs['past_history'];
            $add['person_history'] = $caseIs['personal_history'];
            $add['family_history'] = $caseIs['family_history'];
            $add['physique_history'] = $caseIs['body_check'] == 'null' ? '' : $caseIs['body_check'];
            $add['diagnose'] = $caseIs['diseases_name'] ?? '';
            $add['auxiliary_result'] = $caseIs['assist_check_result'];
            $add['clinic_opinion'] = $caseIs['treat_opinion'];
            $add['patient_diseases_id'] = $caseIs['disease_id'] ? ConfigService::DISEASE[$caseIs['disease_id']] : $caseIs['disease_id'];
            $add['diseases_name'] = $caseIs['disease_name'];
            $add['operate_id'] = self::createMember($caseIs['created_user_id']);
            $add['created_at'] = time();
            $add['updated_at'] = time();
            $addList[] = $add;
        }

        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(MedicalRecords::tableName(),
                    [
                        'project_id', 'patient_user_id', 'sick_time', 'main_desc', 'now_medical_history', 'allergy_history',
                        'jw_history', 'person_history', 'family_history', 'physique_history', 'diagnose', 'auxiliary_result',
                        'clinic_opinion', 'patient_diseases_id', 'diseases_name', 'operate_id', 'created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }
        return true;
    }


    //===================================================================================================

    /**
     * 患者治疗
     */
    public static function treat()
    {
        Model::setTableName('bm_treat');
        $baibaiTreat = Model::find()
            ->where(['status' => 1])
            ->asArray()
            ->all();

        $lastTreatment = UserTreatment::find()
            ->select('id')
            ->orderBy('id desc')
            ->limit(1)
            ->asArray()
            ->one();
        $lastTreatmentId = 0;
        if ($lastTreatment && $lastTreatment['id']) {
            $lastTreatmentId = $lastTreatment['id'];
        }
        self::treatDetail($lastTreatmentId);

        $addList = [];
        foreach ($baibaiTreat as $val) {

            Model::setTableName('bm_patient');
            $baibaiPatient = Model::findOne(['id' => $val['patient_id']]);

            // 患者ID 为3,4,5 和 8,9 的诊疗号相同，在白友系统里诊疗号唯一，所以得修改
            if (in_array($baibaiPatient->id, [3,4,5,8,9])) {
                $baibaiPatient->treat_no = self::havePatient($baibaiPatient->id);
            }

            $add['id'] = $val['id'] + $lastTreatmentId; // 治疗表没数据直接添加ID
            $add['project_id'] = self::$projectId;
            $add['treatment_room_id'] = ConfigService::TREATMENT_ROOM[$val['consultroom_id']];
            $add['patient_card_id'] = $baibaiPatient->treat_no;

            $clinicProject = self::getClinicProject($val['item_id']);
            $add['clinic_project_id'] = $clinicProject['id'];
            $add['clinic_project_name'] = $clinicProject['name'];

            $add['recently_clinic_time'] = $val['finished_at'];
            $add['recently_clinic_num'] = $val['real_num'];
            $add['recently_surplus_num'] = 0;
            $add['clinic_name'] = ConfigService::CLINIC['name'];
            $add['operate_id'] = self::createMember($val['finished_user_id']);
            $add['operate_profession'] = '医助';
            $add['operate_name'] = $val['finished_user_name'];
            $add['patient_signature'] = $val['sign'];
            $add['type'] = UserTreatment::TYPE_TREAT;
            $add['remark'] = $val['doctor_remark'] . '/' . $val['treat_remark'];
            $add['created_at'] = time();
            $add['updated_at'] = time();
            $addList[] = $add;
        }
        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(UserTreatment::tableName(),
                    [
                        'id', 'project_id', 'treatment_room_id', 'patient_card_id', 'clinic_project_id',
                        'clinic_project_name', 'recently_clinic_time', 'recently_clinic_num', 'recently_surplus_num',
                        'clinic_name','operate_id', 'operate_profession', 'operate_name',
                        'patient_signature', 'type', 'remark','created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }
        return true;
    }

    /**
     *  患者治疗详情
     */
    public static function treatDetail($lastTreatmentId)
    {
        Model::setTableName('bm_treat_info');
        $baibaiTreatInfo = Model::find()
            ->asArray()
            ->all();

        $lastTreatmentDetail = UserTreatmentDetail::find()
            ->select('id')
            ->orderBy('id desc')
            ->limit(1)
            ->asArray()
            ->one();
        $lastTreatmentDetailId = 0;
        if ($lastTreatmentDetail && $lastTreatmentDetail['id']) {
            $lastTreatmentDetailId = $lastTreatmentDetail['id'];
        }

        $addList = [];
        foreach ($baibaiTreatInfo as $val) {

            Model::setTableName('bm_treat');
            $baibaiTreat = Model::find()
                ->select('patient_id')
                ->where(['id' => $val['treat_id']])
                ->asArray()
                ->one();

            Model::setTableName('bm_patient');
            $baibaiPatient = Model::findOne(['id' => $baibaiTreat['patient_id']]);

            // 患者ID 为3,4,5 和 8,9 的诊疗号相同，在白友系统里诊疗号唯一，所以得修改
            if (in_array($baibaiPatient->id, [3,4,5,8,9])) {
                $baibaiPatient->treat_no = self::havePatient($baibaiPatient->id);
            }

            $add['id'] = $val['id'] + $lastTreatmentDetailId; // 治疗表没数据直接添加ID
            $add['project_id'] = self::$projectId;
            $add['patient_card_id'] = $baibaiPatient->treat_no;
            $add['clinic_treatment_id'] = $val['treat_id'] + $lastTreatmentId;
            $add['body_parts_large_name'] = $val['body_large_name'];
            $add['body_parts_small_name'] = $val['body_small_name'];
            $add['num'] = $val['num'];
            $add['dose'] = $val['dose'];
            $add['created_at'] = time();
            $add['updated_at'] = time();
            $addList[] = $add;

        }

        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(UserTreatmentDetail::tableName(),
                    [
                        'id', 'project_id', 'patient_card_id', 'clinic_treatment_id', 'body_parts_large_name',
                        'body_parts_small_name', 'num', 'dose', 'created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }
        return true;
    }

    //=====================================================================================================
    /**
     * 患者治疗项目
     */
    public static function userSurplus()
    {
        Model::setTableName('bm_patient_item');
        $baibaiBillInfo = Model::find()
            ->asArray()
            ->all();

        $addList = [];
        foreach ($baibaiBillInfo as $val) {
            Model::setTableName('bm_patient');
            $baibaiPatient = Model::findOne(['id' => $val['patient_id']]);

            // 患者ID 为3,4,5 和 8,9 的诊疗号相同，在白友系统里诊疗号唯一，所以得修改
            if (in_array($baibaiPatient->id, [3,4,5,8,9])) {
                $baibaiPatient->treat_no = self::havePatient($baibaiPatient->id);
            }

            $add['project_id'] = self::$projectId;
            $add['patient_card_id'] = $baibaiPatient->treat_no;
            $add['name'] = $baibaiPatient->real_name;

            $clinicProject = self::getClinicProject($val['item_id']);
            $add['clinic_project_id'] = $clinicProject['id'];

            $add['clinic_project_name'] = $val['item_name'];
            $add['total_num'] = $val['remain_num'];// 这个数量是实际剩余数量，不用减
            $add['help_num'] = 0;
            $add['created_at'] = time();
            $add['updated_at'] = time();
            $addList[] = $add;
        }
        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(UserSurplus::tableName(),
                    [
                        'project_id', 'patient_card_id', 'name',
                        'clinic_project_id', 'clinic_project_name',
                        'total_num', 'help_num', 'created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }
        return true;
    }

    //=====================================================================================================

    /**
     * 患者就诊记录表(患者到院)
     */
    public static function clinicRecord($lastPatientId)
    {
        Model::setTableName('bm_patient');
        $baibaiPatient = Model::find()
            ->select('id, treat_no, real_name')
            ->asArray()
            ->all();

        $addList = [];
        $patientArrive = [];
        foreach ($baibaiPatient as $val) {

            // 患者治疗
            Model::setTableName('bm_treat');
            $baibaiTreatPatient = Model::find()
                ->select('id, patient_id, created_at, created_date')
                ->where(['status' => 1, 'patient_id' => $val['id']])
                ->groupBy('created_date')
                ->asArray()
                ->all();
            $createdDate = array_column($baibaiTreatPatient, 'created_date');

            // 患者到诊
            Model::setTableName('bm_patient_appoint_info');
            $baibaiAppointPatient = Model::find()
                ->select("id, patient_id, in_time")
                ->where(['appoint_status' => 3, 'patient_id' => $val['id']])
                ->asArray()
                ->all();

            // 格式调整
            foreach ($baibaiAppointPatient as $a => &$ap) {
                $ap['created_date'] = date('Y-m-d', $ap['in_time']);
                // 去重（一天一条数据）
                if (in_array($ap['created_date'], $createdDate)) {
                    unset($baibaiTreatPatient[$a]);
                    continue;
                }
                $ap['created_at'] = $ap['in_time'];
                unset($ap['in_time']);
            }

            $patientArrive = array_merge($patientArrive, $baibaiAppointPatient, $baibaiTreatPatient);
        }

        if ($patientArrive) {
            foreach ($patientArrive as $arrive) {
                $add['project_id'] = self::$projectId;
                $add['patient_user_id'] = $lastPatientId + $arrive['patient_id'];
                $add['clinic_day'] = $arrive['created_date'];
                $add['clinic_time'] = $arrive['created_at'] ?? 0;
                $add['is_initial_clinic'] = 0;// 初复诊
                $add['source'] = 3;// 来源白白管家
                $add['created_at'] = time();
                $add['updated_at'] = time();
                $addList[] = $add;
            }
        }

        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(ClinicRecord::tableName(),
                    [
                        'project_id', 'patient_user_id', 'clinic_day', 'clinic_time',
                        'is_initial_clinic', 'source', 'created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }
        return true;
    }

    //=====================================================================================================

    /**
     * 患者消费记录表
     */
    public static function patientConsumption()
    {
        Model::setTableName('bm_bill');
        $baibaiBill = Model::find()
            ->where(['patient_appoint_info_id' => 0])
            ->asArray()
            ->all();

        $lastConsumption = Consumption::find()
            ->select('id')
            ->orderBy('id desc')
            ->limit(1)
            ->asArray()
            ->one();
        $consumptionId = 0;
        if ($lastConsumption && $lastConsumption['id']) {
            $consumptionId = $lastConsumption['id'];
        }

        // 先新增消费详情
        self::patientConsumptionDetail($consumptionId);

        $addList = [];
        foreach ($baibaiBill as $val) {

            Model::setTableName('bm_patient');
            $baibaiPatient = Model::findOne(['id' => $val['patient_id']]);

            // 患者ID 为3,4,5 和 8,9 的诊疗号相同，在白友系统里诊疗号唯一，所以得修改
            if (in_array($baibaiPatient->id, [3,4,5,8,9])) {
                $baibaiPatient->treat_no = self::havePatient($baibaiPatient->id);
            }

            $add['id'] = $lastConsumption ? ($lastConsumption['id'] + $val['id']) : $val['id'];
            $add['project_id'] = self::$projectId;
            $add['patient_card_id'] = $baibaiPatient->treat_no;
            $add['consulting_room'] = $val['clinic_name'];
            $add['doctor_name'] = $val['doctor_real_name'] ?? '';
            $add['clinic_time'] = $val['reception_time'] ?? 0;

            if ($val['type'] == 1) { // 收费单
                $add['money'] = $val['all_cost'];
                $add['reality_money'] = $val['cost'];
                $add['refund_money'] = 0;
            } else { // 退费单
                $add['money'] = 0;
                $add['reality_money'] = -$val['cost'];
                $add['refund_money'] = -$val['cost'];
            }

            $add['trade_type'] = $val['type'];
            $add['remark'] = '';
            $add['created_at'] = time();
            $add['updated_at'] = time();
            $addList[] = $add;
        }

        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(Consumption::tableName(),
                    [
                        'id', 'project_id', 'patient_card_id', 'consulting_room', 'doctor_name', 'clinic_time',
                        'money', 'reality_money', 'refund_money', 'trade_type', 'remark', 'created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }
        return true;
    }

    /**
     * 患者消费记录表
     */
    public static function patientConsumptionDetail($consumptionId)
    {
        Model::setTableName('bm_bill_info');
        $baibaiBillInfo = Model::find()
            ->where(['>', 'id', 0])
            ->asArray()
            ->all();

        $addList = [];
        foreach ($baibaiBillInfo as $val) {

            Model::setTableName('bm_bill');
            $baibaiBill = Model::find()
                ->select('end_time, patient_id')
                ->where(['id' => $val['bill_id']])
                ->asArray()
                ->one();

            Model::setTableName('bm_patient');
            $baibaiPatient = Model::findOne(['id' => $baibaiBill['patient_id']]);

            // 患者ID 为3,4,5 和 8,9 的诊疗号相同，在白友系统里诊疗号唯一，所以得修改
            if (in_array($baibaiPatient->id, [3,4,5,8,9])) {
                $baibaiPatient->treat_no = self::havePatient($baibaiPatient->id);
            }

            $add['project_id'] = self::$projectId;
            $add['consumption_id'] = $consumptionId + $val['bill_id'];
            $add['patient_card_id'] = $baibaiPatient->treat_no;

            $add['yiyuan_aid'] = $val['id'];
            $add['drug_name'] = $val['treat_name'];
            $add['drug_num'] = $val['num'];
            $add['drug_unit'] = $val['unit'];
            $add['drug_type'] = $val['type'];
            $add['price'] = $val['price'];
            $add['money'] = $val['cost'];
            $add['discount_rate'] = 1;
            $add['reality_money'] = $val['cost'];
            $add['consumption_type'] = 1;
            $add['remark'] = '';
            $add['event_at'] = $baibaiBill['end_time'];

            $add['created_at'] = time();
            $add['updated_at'] = time();
            $addList[] = $add;
        }
        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(ConsumptionDetail::tableName(),
                    [
                        'project_id', 'consumption_id', 'patient_card_id', 'yiyuan_aid', 'drug_name',
                        'drug_num', 'drug_unit', 'drug_type', 'price', 'money', 'discount_rate', 'reality_money',
                        'consumption_type', 'remark', 'event_at', 'created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }
        return true;
    }


    /**
     * 患者诊疗号重复(在患者已有诊疗号后加ID，只针对诊疗号为 7310119072902、7310119072905)
     * $patientId 白白管家患者表ID 3,4,5 => 7310119072902  8,9 => 7310119072905
     */
    public static function havePatient($patientId)
    {
        if ($patientId == 3) {
            return $treatNo = 73101190729023;
        } else if ($patientId == 4) {
            return $treatNo = 73101190729024;
        } elseif ($patientId == 5) {
            return $treatNo = 73101190729025;
        } elseif ($patientId == 8) {
            return $treatNo = 73101190729058;
        } elseif ($patientId == 9)  {
            return $treatNo = 73101190729059;
        } else {
            Model::setTableName('bm_patient');
            $patientUser = Model::findOne(['id' => $patientId]);
            return $patientUser->treat_no ?? 0;
        }
    }


    /**
     * 新增后台人员(操作人员不存在则新增)
     */
    public static function createMember($memberId)
    {
        Model::setTableName('bm_admin');
        $adminIs = Model::findOne(['user_id' => $memberId]);

        $memberIs = Member::find()
            ->alias('a')
            ->leftJoin(Attr::tableName() . ' b', 'a.id = b.member_id')
            ->where(['a.realname' => $adminIs->name, 'b.project_id' => self::$projectId])
            ->one();
        if (!$memberIs) {
            Model::setTableName('bm_admin');
            $admin = Model::findOne(['name' => $adminIs->name]);

            $member = new Member();
            $member->username = $admin->name;
            $member->mobile = $admin->phone;
            $member->password_hash = '$2y$13$89Wn/gAB9AQrjYxzm141U.Kpw6nOhiU17KwzoOPI0jYmXtH5A5O0O';
            $member->realname = $admin->name;
            $member->is_super = Member::IS_SUPER_NO;
            $member->save();

            $attr = new Attr();
            $attr->member_id = $member->id;
            $attr->project_id = self::$projectId;
            $attr->department_id = 85;
            $attr->clinic_id = ConfigService::CLINIC['id'];
            $attr->profession_id = 301;
            $attr->profession_name = '医助';
            $attr->save();

            return $member->id;
        }
        return $memberIs->id;
    }

    /**
     * 治疗项目
     *
     * @param $clinicProjectId
     * @return array
     */
    public static function getClinicProject($clinicProjectId)
    {
        Model::setTableName('bm_item');
        $baibaiclinicProject = Model::findOne(['id' => $clinicProjectId]);

        if (!$baibaiclinicProject || !$baibaiclinicProject->name) {
            return ['id' => 0, 'name' => ''];
        }
        $baiyouclinicProject = Project::findOne(['project_id' => self::$projectId, 'name' => $baibaiclinicProject->name]);
        if (!$baiyouclinicProject) {
            $baiyouclinicProject = new Project();
            $baiyouclinicProject->name = $baibaiclinicProject->name;
            $baiyouclinicProject->project_id = self::$projectId;
            $baiyouclinicProject->bind_yiyuan_name = $baibaiclinicProject->name;
            $baiyouclinicProject->status = Project::STATUS_ENABLE;
            $baiyouclinicProject->template = $baibaiclinicProject->treat_template ? $baibaiclinicProject->treat_template : 3;
            $baiyouclinicProject->created_at = time();
            $baiyouclinicProject->updated_at = time();
            $baiyouclinicProject->save();
        }

        return ['id' => $baiyouclinicProject->id ?? 0, 'name' => $baiyouclinicProject->name ?? ''];
    }

    /**
     * 病种
     *
     * @param $diseaseId
     * @return int
     */
    public static function getDisease($diseaseId)
    {
        Model::setTableName('bm_disease');
        $baibaiDisease = Model::findOne(['id' => $diseaseId]);

        if (!$baibaiDisease || !$baibaiDisease->name) {
            return 0;
        }
        $baiyouDisease = PatientDiseases::findOne(['pid' => 0, 'diseases_name' => $baibaiDisease->name]);
        return $baiyouDisease->id ?? 0;
    }


    /**
     * 患者消费、治疗、来院记录表
     *
     * @param $params
     * @return bool|int
     */
    public static function patientRevisitingRecordTreat()
    {
        $whereTre = ['and'];
        $whereTre[] = ['=', 'status', 1];

        // 按时间的所有治疗数据
        Model::setTableName('bm_treat');
        $baibaiTreat = Model::find()
            ->where($whereTre)
            ->asArray()
            ->all();

        $lastTreatment = RevisitingRecord::find()
            ->select('id')
            ->orderBy('id desc')
            ->limit(1)
            ->asArray()
            ->one();
        $lastTreatmentId = 0;
        if ($lastTreatment && $lastTreatment['id']) {
            $lastTreatmentId = $lastTreatment['id'];
        }

        $addList = [];
        foreach ($baibaiTreat as $key => $val) {

            Model::setTableName('bm_patient');
            $baibaiPatient = Model::findOne(['id' => $val['patient_id']]);

            $add['id'] = $val['id'] + $lastTreatmentId; // 治疗表没数据直接添加ID
            $add['project_id'] = self::$projectId;
            $add['clinic_id'] = self::getPatientClinicOrReg($val['patient_id'], 1);;
            $add['patient_card_id'] = $baibaiPatient->treat_no;
            $add['revisting_time'] = $val['created_at'];
            $add['source_id'] = $val['id'];
            $add['type'] = RevisitingRecord::TYPE_TREAT;

            $add['is_sum'] = RevisitingRecord::IS_SUM_NO;
            $add['day_time'] = strtotime($val['created_date']);

            $add['created_at'] = time();
            $add['updated_at'] = time();
            $addList[] = $add;
        }

        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(RevisitingRecord::tableName(),
                    [
                        'id', 'project_id', 'clinic_id', 'patient_card_id', 'revisting_time',
                        'source_id', 'type', 'is_sum', 'day_time', 'created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }

        return true;

    }


    /**
     * 患者消费、治疗、来院记录表
     *
     * @param $params
     * @return bool|int
     */
    public static function patientRevisitingRecordBill()
    {
        $whereTre = ['and'];
        $whereTre[] = ['patient_appoint_info_id' => 0, 'status' => 1];

        // 按时间的所有消费数据
        Model::setTableName('bm_bill');
        $baibaibill = Model::find()
            ->where($whereTre)
            ->asArray()
            ->all();

        $lastbill = RevisitingRecord::find()
            ->select('id')
            ->orderBy('id desc')
            ->limit(1)
            ->asArray()
            ->one();
        $lastbillId = 0;
        if ($lastbill && $lastbill['id']) {
            $lastbillId = $lastbill['id'];
        }

        $addList = [];
        foreach ($baibaibill as $key => $val) {

            Model::setTableName('bm_patient');
            $baibaiPatient = Model::findOne(['id' => $val['patient_id']]);

            $add['id'] = $val['id'] + $lastbillId; // 消费表没数据直接添加ID
            $add['project_id'] = self::$projectId;
            $add['clinic_id'] = self::getPatientClinicOrReg($val['patient_id'], 1);;
            $add['patient_card_id'] = $baibaiPatient->treat_no;
            $add['revisting_time'] = $val['created_at'];
            $add['source_id'] = $val['id'];
            $add['type'] = RevisitingRecord::TYPE_CONSUME;

            $add['is_sum'] = RevisitingRecord::IS_SUM_NO;
            $add['day_time'] = strtotime($val['created_date']);

            $add['created_at'] = time();
            $add['updated_at'] = time();
            $addList[] = $add;
        }

        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(RevisitingRecord::tableName(),
                    [
                        'id', 'project_id', 'clinic_id', 'patient_card_id', 'revisting_time',
                        'source_id', 'type', 'is_sum', 'day_time', 'created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }

        return true;

    }

    /**
     * 更新来院记录表的is_sum字段（一个患者一天只记录一次）
     *
     * @return bool|int
     */
    public static function updateIsSum()
    {
        $revisitingRecord = RevisitingRecord::find()
            ->select('day_time, patient_card_id, id')
            ->where([
                'and',
                ['project_id' => self::$projectId],
            ])
            ->groupBy('day_time, patient_card_id')
            ->asArray()
            ->all();

        if ($revisitingRecord) {
            // 新增患者月到诊统计记录表
            self::createPatientMonthRevisitingRecord();

            $updateIds = array_column($revisitingRecord, 'id');
            RevisitingRecord::updateAll(['is_sum' => RevisitingRecord::IS_SUM_YES], ['id' => $updateIds]);
            return true;
        }
        return true;
    }

    /**
     * 患者月到诊统计记录表
     *
     * @return bool|int
     */
    public static function createPatientMonthRevisitingRecord()
    {
        // 按照患者诊疗号及月份分组
        $revisitingRecord = RevisitingRecord::find()
            ->select([
                'patient_card_id', 'id', 'project_id',
                new Expression('FROM_UNIXTIME(`day_time`, "%Y-%m") day_date'),
                'day_time', 'clinic_id'
            ])
            ->where(['project_id' => self::$projectId])
            ->groupBy('day_date, patient_card_id')
            ->orderBy(['id' => SORT_DESC])
            ->asArray()
            ->all();

        $addList = [];
        foreach ($revisitingRecord as $val) {
            if (MonthRevisitingRecord::findOne([
                'project_id' => $val['project_id'],
                'patient_card_id' => $val['patient_card_id'],
                'month' => $val['day_time']])) {
                continue;
            }

            $add['project_id'] = self::$projectId;
            $add['clinic_id'] = $val['clinic_id'];
            $add['patient_card_id'] = $val['patient_card_id'];
            $add['month'] = $val['day_time'];
            $add['status'] = MonthRevisitingRecord::STATUS_ENABLE;

            $add['created_at'] = time();
            $add['updated_at'] = time();
            $addList[] = $add;

        }
        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(MonthRevisitingRecord::tableName(),
                    [
                        'project_id', 'clinic_id', 'patient_card_id', 'month',
                        'status', 'created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }

        return true;

    }

    /**
     * 白白管家部位管理
     */
    public static function createBbBodyParts()
    {
        // 大部位
        Model::setTableName('bm_body_large');
        $bodyLarge = Model::find()->asArray()->all();

        $addList = [];
        foreach ($bodyLarge as $val) {
            $add['pid'] = 0;
            $add['body_parts_name'] = $val['name'];
            $add['sort'] = $val['sort'];
            $add['status'] = $val['status'];

            $add['created_at'] = time();
            $add['updated_at'] = time();
            $addList[] = $add;
        }

        Model::setTableName('bm_body_small');
        $body = Model::find()
            ->alias('a')
            ->select('a.*, bm_body_relation.body_large_id')
            ->leftJoin('bm_body_relation', 'a.id = bm_body_relation.body_small_id')
            ->asArray()
            ->all();

        foreach ($body as $val) {
            $add['pid'] = $val['body_large_id'];
            $add['body_parts_name'] = $val['name'];
            $add['sort'] = $val['sort'];
            $add['status'] = $val['status'];

            $add['created_at'] = time();
            $add['updated_at'] = time();
            $addList[] = $add;
        }

        if ($addList) {
            return Yii::$app->db->createCommand()
                ->batchInsert(BbBodyParts::tableName(),
                    [
                        'pid', 'body_parts_name', 'sort',
                        'status', 'created_at', 'updated_at'
                    ],
                    $addList)
                ->execute();
        }

        return true;
    }

}